
#include <stdio.h>
#include <unistd.h>

#include "ohos_init.h"
#include "cmsis_os2.h"
#include "hi_io.h" 
#include "hi_gpio.h" 
#include "globalvars.h"
#include "smartled.h"

int g_led_status = 0; //灯光的状态


// int g_led_control_flag = 1;
// int g_led_work_mode = 1;
// int g_led_color = 0;
// int g_led_flag = 0;


static void SmartLed_Task(void)
{
    Led_init(); //初始化门模块  
    printf("The led_task is activated successfully!!\r\n");
    usleep(100 * 1000);  //延时100ms
	
    while(1)
    {   
        if(g_led_work_mode == 1)
        {
            if(g_led_status == 0 && g_led_control_flag == 1)
            {
                Led_switch(1, g_led_color);  //开灯
				// usleep(3 * 1000 * 1000);
                g_led_status = 1;
            }
            if(g_led_status == 1 && g_led_control_flag == 0)
            {
                Led_switch(0, 0); // 关灯
				// usleep(3 * 1000 * 1000);
                g_led_status = 0;
            }
        }
        if(g_led_work_mode == 2)
        {
            if(g_led_status == 1 && g_led_flag == 1)
            {
                Led_switch(0, 0); //关灯
				// usleep(3 * 1000 * 1000);
                g_led_status = 0;
            }
            if(g_led_status == 0 && g_led_flag == 0)
            {
                Led_switch(1, g_led_color); //开灯
				// usleep(3 * 1000 * 1000);
                g_led_status = 1;
            }
        }

        if(g_led_work_mode == 3)
        {
            if(g_led_status == 0 && g_led_control_flag == 1 && g_led_flag == 0)
			{
				Led_switch(1, g_led_color);
				// usleep(3 * 1000 * 1000);
				g_led_status = 1;
			}
			if(g_led_status == 1 && g_led_control_flag == 0 && g_led_flag == 1)
			{
				Led_switch(0, 0);
				// usleep(3 * 1000 * 1000);
				g_led_status = 0;
			}
			if(g_led_status == 1 && g_led_flag == 1)
			{
				Led_switch(0, 0);
				// usleep(3 * 1000 * 1000);
				g_led_status = 0;
			}
			if(g_led_status == 0 && g_led_flag == 0)
			{
				Led_switch(1, g_led_color);
				// usleep(3 * 1000 * 1000);
				g_led_status = 1;
			}
        }
        usleep(3 * 1000 * 1000);
    }
}

static void SmartLed_Entry(void)
{
    printf("\r\n!!!!SmartLed_Task!\r\n");
    /** 任务属性的结构体 **/
    osThreadAttr_t attr;

    attr.name = "SmartLed_Task";         //任务名称
    attr.attr_bits = 0U;           //任务的属性位
    attr.cb_mem = NULL;            //任务控制块的内存初始地址，默认为系统自动分配
    attr.cb_size = 0U;             //任务控制块的内存大小
    attr.stack_mem = NULL;         //任务的内存初始地址，默认为系统自动分配
    attr.stack_size = 4096;        //任务的内存大小
    attr.priority = 11;            //任务的优先级

    /** 创建任务 **/
    if (osThreadNew((osThreadFunc_t)SmartLed_Task, NULL, &attr) == NULL) {
        printf("[SmartLed_Entry] Falied to create SmartLed_Task!\n");
    }
}

/** 让系统运行指定的函数 **/
SYS_RUN(SmartLed_Entry);